var app = getApp();
var util = require('../../utils/util.js');
Page({
  data: {
    user: null,
    //用户喵文记录
    userTalkList: [],
    userTalkList1: [],
    userTalkList2: [],
    //当前是日志还是送养
    mySel: 1,
    //送养记录
    userGiveList: [],
    userGiveList1: [],
    userGiveList2: [],
  },
  onLoad: function(options) {
    //找到系统状态位置
    this.setData({
      statusBarHeight: app.globalData.systemInfo.statusBarHeight + 15
    })
    this.readUserInfo(options.id);
   
  },

  //喵文列表
  getTalkListByUserId: function() {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + 'getTalkListByUserId',
      data: {
        userId: that.data.user.id
      },
      success: function(r) {
        if (r.data.code == 200) {
          var data = r.data.data;
          //左边数组
          var arr1 = [];
          //右边数组
          var arr2 = [];
          for (var i = 0; i < data.length; i++) {
            if (data[i].talkType != 0) {
              data[i].src = data[i].src + "?x-oss-process=video/snapshot,t_1000,f_jpg,w_" + data[i].width + ",h_" + data[i].height + ",m_fast";
            }
            //找到图片高度
            data[i].imageHeight = util.getImageHeight(data[i].height, data[i].width, 0.48);
            data[i].talkType = 0;

            //如果是双数则放到第一个盒子
            if (i % 2 == 0) {
              arr1.push(data[i]);
            } else {
              arr2.push(data[i]);
            }
          }
          //如果是下拉则清空
          if (that.data.refresh) {
            that.setData({
              userTalkList: data,
              userTalkList1: arr1,
              userTalkList2: arr2,
              refresh: false
            })
            wx.stopPullDownRefresh();
          } else {
            if (data.length == 0) {

            } else {
              that.setData({
                userTalkList: that.data.userTalkList.concat(data),
                userTalkList1: that.data.userTalkList1.concat(arr1),
                userTalkList2: that.data.userTalkList2.concat(arr2)
              })
            }
          }
        } else {
          wx.showToast({
            title: '获取日志记录失败',
            icon: 'none',
            duration: 3000
          })
        }
      },
      fail: function() {
        wx.showToast({
          title: '网络断开连接,获取日志记录失败',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },

  //用户送养记录
  getGiveListByUserId: function() {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + 'getGiveListByUserId',
      data: {
        userId:that.data.user.id
      },
      success: function(r) {
        if (r.data.code == 200) {
          var data = r.data.data;
          //左边数组
          var arr1 = [];
          //右边数组
          var arr2 = [];
          for (var i = 0; i < data.length; i++) {
            //找到图片高度
            data[i].imageHeight = util.getImageHeight(data[i].height, data[i].width, 0.48);
            data[i].talkType = 0;

            //如果是双数则放到第一个盒子
            if (i % 2 == 0) {
              arr1.push(data[i]);
            } else {
              arr2.push(data[i]);
            }
          }
          //如果是下拉则清空
          if (that.data.refresh) {
            that.setData({
              userGiveList: data,
              userGiveList1: arr1,
              userGiveList2: arr2,
              refresh: false
            })
            wx.stopPullDownRefresh();
          } else {
            if (data.length == 0) {

            } else {
              that.setData({
                userGiveList: that.data.userGiveList.concat(data),
                userGiveList1: that.data.userGiveList1.concat(arr1),
                userGiveList2: that.data.userGiveList2.concat(arr2)
              })
            }
          }
        } else {
          wx.showToast({
            title: '获取送养记录失败',
            icon: 'none',
            duration: 3000
          })
        }
      },
      fail: function() {
        wx.showToast({
          title: '网络断开连接,获取送养记录失败',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },
  //获取用户信息
  readUserInfo: function(id) {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + 'readUserInfo',
      data: {
        userId: id
      },
      success: function(r) {
        console.log(r)
        if (r.data.code == 200) {
          var user = r.data.data;
          user.showConcernQuantity = util.formatQuantity(user.concernQuantity);
          user.showFansQuantity = util.formatQuantity(user.fansQuantity);
          user.showLikeQuantity = util.formatQuantity(user.likeQuantity);
          that.setData({
            user: user
          })
          that.getTalkListByUserId();
          that.getGiveListByUserId();
        } else {
          wx.showToast({
            title: '查看用户失败',
            icon: 'none',
            duration: 3000
          })
        }
      },
      fail: function() {
        wx.showToast({
          title: '网络断开连接,查看用户失败',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },
  //日志 送养切换
  selMyPush: function(e) {
    var type = e.currentTarget.dataset.type;
    if (this.data.mySel == type) {
      return;
    } else {
      this.setData({
        mySel: type
      })
    }
  },
  //返回下一个页面
  rollback: function() {
    wx.navigateBack({
      delta: 1
    })
  },
  //进入聊天
  chatInfo: function() {
    var senderId = app.globalData.user.plainText;
    console.log(senderId)
    var consigneeId = this.data.user.id;
    console.log(consigneeId)
    var chat = null;
    for (var i of app.globalData.chat) {
      if (i.id == senderId + consigneeId || i.id == consigneeId + senderId) {
        chat = i;
      }
    }
    if (chat == null) {
      var chat = {
        id: senderId + consigneeId,
        userId: consigneeId,
        nikeName: this.data.user.nikeName,
        headPortrait: this.data.user.headPortrait,
        chatList: []
      }
      app.globalData.chat.push(chat);
    }
    wx.navigateTo({
      url: '/pages/chatInfo/chatInfo?id=' + chat.id
    })
  },
})